export default function($scope) {
    let params = {
        createHasInput: false,  // 输入内容是否已经创建任务
    }
    $scope.todos = [{
        task: "学习Mongodb",
        isCompleted: false,
        isEditing: false,
    }, {
        task: "学习Express",
        isCompleted: false,
        isEditing: false,
    }, {
        task: "学习Angular",
        isCompleted: false,
        isEditing: false,
    }, {
        task: "学习Node",
        isCompleted: true,
        isEditing: false,
    }, ];

    $scope.onEditBtnClick = todo => {
        todo.isEditing = true;
    }

    $scope.onCompletedClick = todo => {
        todo.isCompleted = !todo.isCompleted;
    }

    $scope.createTask = () => {
        params.createHasInput = false;  // 置状态为未创建(避免被判断为用户删除输入内容)
        $scope.createTaskInput = "";
    }

    $scope.$watch('createTaskInput', val => {
        //输入框内容不为空, 且未创建任务
        if (val && !params.createHasInput){
            $scope.todos.push({task: val, isCompleted:false});
            params.createHasInput = true;  // 已创建
        }
        // 输入框内容不为空, 且已创建任务
        else if (val && params.createHasInput){
            $scope.todos[$scope.todos.length - 1].task = val;
        }
        // 输入内容为空, 且未创建(即删除输入框内容)
        else if (!val && params.createHasInput){
            $scope.todos.pop();
            params.createHasInput = false;
        }
    });
}
